package cn.xiaopengstack.domain.activity.service.discount.impl;

import cn.xiaopengstack.domain.activity.model.dto.DiscountCalculateDTO;
import cn.xiaopengstack.domain.activity.service.discount.AbstractDiscountCalculateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author jiangyangang
 */
@Slf4j
@Service("MJ")
public class MJCalculateService extends AbstractDiscountCalculateService {

    @Override
    protected BigDecimal doCalculate(DiscountCalculateDTO calculateDTO) {
        log.info("优惠策略折扣计算:{}", calculateDTO.getGroupBuyDiscount().getDiscountType());
        // 解析表达式
        String marketExpr = calculateDTO.getGroupBuyDiscount().getMarketExpr();
        String[] splitExpr = marketExpr.split(",");
        BigDecimal thresholdMoney = new BigDecimal(splitExpr[0]);
        BigDecimal discountMoney = new BigDecimal(splitExpr[1]);

        if (thresholdMoney.compareTo(calculateDTO.getOriginalPrice()) > 0) {
            return calculateDTO.getOriginalPrice();
        }

        return coverMinPrice(discountMoney, new BigDecimal("0.01"));
    }
}
